package com.wondream.myframework.app.vo.base;

import lombok.Data;
import org.apache.commons.lang3.StringUtils;

/**
 * 返回包装类
 */
@Data
public class OpResponse<T> {

    public static final String SUCCESS = "0";
    private String responseCode;
    private String responseMsg;
    private T data;

    /**
     * 构建返回对象
     * @return OpResponse
     */
    public static <T> OpResponse build(String code, String msg, T data) {
        OpResponse<T> result = new OpResponse();
        result.setResponseCode(code);
        result.setResponseMsg(msg);
        result.setData(data);
        return result;
    }

    /**
     * 判断是否成功
     * @return boolean
     */
    public boolean isSuccess() {
        return StringUtils.equals(SUCCESS, this.responseCode);
    }

    /**
     * 创建返回实例
     * @return boolean
     */
    public static <T> OpResponse newInstance(String code, String msg, T data) {
        return build(code, msg, data);
    }

    /**
     * 获取成功响应对象
     * @return
     */
    public static <T> OpResponse success(T data){
        return build(SUCCESS, null, data);
    }

    /**
     * 获取成功响应对象-无响应数据
     * @return
     */
    public static <T> OpResponse success(){
        return success(null);
    }

    /**
     * 构建失败响应对象
     * @return
     */
    public static <T> OpResponse fail(String errorCode, String errorMsg){
        return build(errorCode, errorMsg, null);
    }


}
